package com.medicinemanager.app;

import java.util.Stack;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

public class AppManager {
	private static Stack<Activity> mActivityStack = null;
	private static AppManager mAppManager = null;
	
	private AppManager(){
		mActivityStack = new Stack<Activity>();
	}
	
	public static AppManager getInstance(){
		if(mActivityStack == null){
			synchronized(AppManager.class){
				if(mActivityStack == null){
					mAppManager = new AppManager();
				}
			}
		}
		return mAppManager;
	}
	
	public void addActivity(Activity activity){
		mActivityStack.add(activity);
	}
	
	public void finishActivity(Activity activity){
		mActivityStack.remove(activity);
		activity.finish();
	}
	
	public void finishAllActivity(){
		for(int i = 0; i < mActivityStack.size(); i++){
			mActivityStack.get(i).finish();
		}
		mActivityStack.clear();
	}
	
	@SuppressWarnings("deprecation")
	public void appExit(Context context){
		try{
			finishAllActivity();
			ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
			activityManager.restartPackage(context.getPackageName());
			System.exit(0);
		}catch(Exception e){
		}
		}
}
